import { Sprite } from "@/sprites/Sprite.js";
import { ImageManager } from "@/managers/ImageManager.js";

export class Sprite_Balloon extends Sprite {
    constructor() {
        super();
        this._balloonId = 0;
        this._duration = 0;
        this.anchor.x = 0.5;
        this.anchor.y = 1;
        this.zIndex = 7;

        this.frameIndex=0;
        this.acculumator=0;

        this.isReady=false;

        this.waitCount=0;
        ImageManager.loadSystem('Balloon').then(source=>{
            this.isReady=true;
            this.source=source;
            this.updateFrame();
        });
    }

    setup(balloonId) {
        this._balloonId = balloonId;
        this.frameIndex=0;
        this.acculumator=0;
        this.waitCount=4;
        if(this.isReady){
            this.updateFrame();
            this.visible=true;
        }
    }

    update() {
        if(this.isReady&&this.visible){
            super.update();
            
            ++this.acculumator;
            if(this.acculumator>=8){
                if(this.frameIndex<7){
                    ++this.frameIndex;
                    this.updateFrame();
                    return;
                }else if(this.waitCount>0){
                    --this.waitCount;
                }else{
                    this.visible=false;
                }
                this.acculumator=0;
            }
        }
    }

    updateFrame() {
        if(this.balloonId>0){
            let w = 48;
            let h = 48;
            let sx = this.frameIndex * w;
            let sy = (this._balloonId - 1) * h;
            this.setFrame(sx, sy, w, h);
        }
    }

    isPlaying() {return this.visible;}
}